<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { CaseHistory } from '#/api/business/case-history/model';

import { Page, useVbenModal } from '@vben/common-ui';
import { getVxePopupContainer } from '@vben/utils';

import { message, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  caseHistoryList,
  caseHistoryRemove,
} from '#/api/business/case-history';

import caseHistoryModal from './case-history-modal.vue';
import { columns } from './data';

const gridOptions: VxeGridProps = {
  columns,
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        return await caseHistoryList({
          pageNo: page.currentPage,
          pageSize: page.pageSize,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'case-history-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  showSearchForm: false,
  gridOptions,
});

const [CaseHistoryModal, modalApi] = useVbenModal({
  connectedComponent: caseHistoryModal,
});

function handleAdd() {
  modalApi.setData({}).open();
}

async function handleEdit(record: CaseHistory) {
  modalApi.setData({ id: record.id }).open();
}

async function handleDelete(row: CaseHistory) {
  await caseHistoryRemove(row.id);
  message.success('删除成功');
  await tableApi.query();
}
</script>

<template>
  <Page :auto-content-height="true">
    <CaseHistoryModal @reload="tableApi.query()" />
    <BasicTable table-title="病例管理">
      <template #toolbar-tools>
        <Space>
          <a-button
            type="primary"
            @click="handleAdd"
            v-access:code="['medical:add']"
          >
            {{ $t('pages.common.add') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button
            @click.stop="handleEdit(row)"
            v-access:code="['medical:update']"
          >
            {{ $t('pages.common.edit') }}
          </ghost-button>
          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              @click.stop=""
              v-access:code="['medical:delete']"
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
  </Page>
</template>
